package com.his.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.his.entity.Drug;
import com.his.entity.NonDrug;
import com.his.entity.ResponseResult;
import com.his.mapper.NonDrugMapper;
import com.his.service.INonDrugService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 非药品收费项目表 服务实现类
 * </p>
 *
 * @author Demoon Liu
 * @since 2022-02-20
 */
@Service
public class NonDrugServiceImpl extends ServiceImpl<NonDrugMapper, NonDrug> implements INonDrugService {

    @Resource
    private NonDrugMapper mapper;

    @Override
    public ResponseResult myInsert(NonDrug nonDrug) {
        Long count = 1L;
        Integer code = 0;
        String message = "";

        try {
            NonDrug repeat = mapper.selectOne(new QueryWrapper<NonDrug>().eq("name", nonDrug.getName()));
            if (null != repeat) throw new Exception("已存在相同的非药品");

            count = mapper.getMaxId();
            nonDrug.setId(count+1);
            nonDrug.setStatus(1);
            nonDrug.setCreatedate(LocalDateTime.now());
            if (0 == mapper.insert(nonDrug)) {
                throw new Exception();
            }

            message = "插入成功";
            code = 201;
        }
        catch (Exception err) {
            message = "插入失败";
            code = 400;
        }

        return new ResponseResult(code, message, nonDrug);
    }

    @Override
    public ResponseResult myUpdate(NonDrug nonDrug) {
        Integer code = 0;
        String message = "";

        try {

            if (0 == mapper.updateById(nonDrug)) {
                throw new Exception();
            }

            message = "修改成功";
            code = 201;
        }
        catch (Exception err) {
            message = "修改失败";
            code = 400;
        }

        return new ResponseResult(code, message, nonDrug);
    }

    @Override
    public ResponseResult mySearchAll() {
        Integer code = 0;
        String message = "";
        IPage<NonDrug> page = new Page<NonDrug>(1, 10);
        IPage<NonDrug> result = null;

        try {
            result = mapper.selectPage(page, new QueryWrapper<NonDrug>());
            message = "查询成功";
            code = 201;

        }
        catch (Exception err) {
            message = "查询失败";
            code = 400;
        }

        return new ResponseResult(code, message, result);
    }

    @Override
    public ResponseResult myFuzzySearch(String str) {
        Integer code = 0;
        String message = "";
        IPage<NonDrug> page = new Page<NonDrug>(1, 10);
        IPage<NonDrug> result = null;

        try {
            result = mapper.selectPage(page, new QueryWrapper<NonDrug>().like("code", str)
                    .or().like("name", str)
                    .or().like("mnemoniccode", str)
            );
            message = "模糊查询成功";
            code = 201;



        }
        catch (Exception err) {
            message = "模糊查询失败";
            code = 400;
        }

        return new ResponseResult(code, message, result);
    }

    @Override
    public ResponseResult myDelete(NonDrug nonDrug) {
        Integer code = 0;
        String message = "";

        try {
            if (0 == mapper.deleteById(nonDrug)) {
                throw new Exception();
            }
            message = "删除成功";
            code = 201;



        }
        catch (Exception err) {
            message = "删除失败";
            code = 400;
        }

        return new ResponseResult(code, message, null);
    }
}
